package com.factory.iamp.dao;

import com.factory.iamp.pojo.Group;
import com.factory.iamp.pojo.UserGroup;
import com.factory.iamp.pojo.UserList;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface GroupMapper {
    @Select("select count(gid) from `group`")
    int Count();

    @Select("select `count` from `group` where gid = #{gid}")
    int groupMemberCount(int gid);

    @Select("select count(gid) from `group` where name like #{name}")
    int findByNameCount(@Param("name") String name);

    @Select("select * from `group` where gid = #{gid} limit 1")
    Group findById(@Param("gid") int gid);

    @Select("select * from `group` where name = #{name} limit 1")
    Group findByName(@Param("name") String name);

    @Select("select * from user_group where gid = #{gid} and uid = #{uid}")
    Group checkDuplicate(@Param("gid") int gid, @Param("uid") String uid);

    @Select("select * from `group` limit #{offset}, #{limit}")
    List<Group> ListPaged(@Param("offset") int offset, @Param("limit") int limit);

    @Select("select * from `group` where gid in (select gid from user_group where uid = #{uid})")
    List<Group> findByUid(@Param("uid") String uid);

    @Select("select * from `group` where name like #{name} limit #{offset}, #{limit}")
    List<Group> findByNameListPaged(@Param("offset") int offset, @Param("limit") int limit, @Param("name") String name);

    @Select("select * from user where uid in (select uid from user_group where gid = #{gid}) limit #{offset}, #{limit}")
    @Results({
            @Result(property = "role", column = "role", one = @One(select = "com.factory.iamp.dao.RoleMapper.findById"))
    })
    List<UserList> groupMemberListPaged(@Param("offset") int offset, @Param("limit") int limit, @Param("gid") int gid);

    @Insert("insert into `group`(name, count) values (#{name}, #{count})")
    @Options(useGeneratedKeys = true, keyProperty = "gid", keyColumn = "gid")
    void addGroup(Group group);

    @Insert("insert into user_group(gid, uid) values (#{gid}, #{uid})")
    void addUser(@Param("gid") int gid, @Param("uid") String uid);

    @Insert({
            "<script>",
            "insert into user_group(gid, uid) values",
            "<foreach collection = 'userList' item = 'item' index = 'index' separator = ','>",
            "(#{item.gid}, #{item.uid})",
            "</foreach>",
            "</script>"
    })
    void addUserList(@Param("userList") List<UserGroup> userList);

    @Update("update `group` set `count` = `count` + 1 where gid = #{gid}")
    void addCount(@Param("gid") int gid);

    @Update("update `group` set `count` = #{count} where gid = #{gid}")
    void alterCount(@Param("gid") int gid, @Param("count") int count);

    @Delete("delete from user_group where gid = #{gid}")
    void deleteRelation(@Param("gid") int gid);

    @Delete("delete from `group` where gid = #{gid}")
    void deleteGroup(@Param("gid") int gid);
}
